package org.micode.common.config.reader;

import org.micode.common.exception.InvalidClassException;
import org.micode.common.utils.ObjectUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

@Slf4j
public class ConfigReaderFactory {

    private static IConfigReader globalConfigReader;

    public static IConfigReader configReader(String implClassName) {
        if (StringUtils.isEmpty(implClassName)) {
            if (null == globalConfigReader) {
                globalConfigReader = new DefaultConfigReader();
            }
        }

        try {
            if (null == globalConfigReader) {
                globalConfigReader = ObjectUtils.createByClassName(implClassName);
            }
            return globalConfigReader;
        } catch (Exception e) {
            log.error("Instantiation exception of IConfigReader({})", implClassName, e);
            throw new InvalidClassException("Instantiation exception of IConfigReader");
        }
    }

}
